package com.example.leo.mycharmingstudent.a0829practice;

import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.a0829practice.fragment.ClazzDemoFragment;

public class ClazzDemoActivity extends AppCompatActivity
        implements View.OnClickListener, ClazzDemoFragment.OnClazzDemoListener {

    //标题
    private TextView tvTitle;
    //fragment容器布局
    private FrameLayout flContainer;
    //首页标签
    private TextView tvMainPage;
    //内容标签
    private TextView tvContent;
    //我的标签
    private TextView tvMine;
    private ClazzDemoFragment mMainPageFragment;
    private ClazzDemoFragment mContentFragment;
    private ClazzDemoFragment mMineFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clazz_demo_activity);
        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        tvTitle = findViewById(R.id.tv_title_clazz_demo_activity);
        flContainer = findViewById(R.id.fl_container_clazz_demo_activity);
        tvMainPage = findViewById(R.id.tv_main_page_clazz_demo_activity);
        tvContent = findViewById(R.id.tv_content_clazz_demo_activity);
        tvMine = findViewById(R.id.tv_mine_clazz_demo_activity);
    }

    private void initData() {
        mMainPageFragment = ClazzDemoFragment
                .createClazzDemoFragment(
                        getResources().getString(R.string.main_page));
        mContentFragment = ClazzDemoFragment
                .createClazzDemoFragment(
                        getResources().getString(R.string.content));
        mMineFragment = ClazzDemoFragment
                .createClazzDemoFragment(
                        getResources().getString(R.string.mine));
        mMainPageFragment.setOnClazzDemoListener(this);
        mContentFragment.setOnClazzDemoListener(this);
        mContentFragment.setOnClazzDemoListener(this);

        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_container_clazz_demo_activity, mMainPageFragment, "main")
                .add(R.id.fl_container_clazz_demo_activity, mContentFragment, "content")
                .add(R.id.fl_container_clazz_demo_activity, mMineFragment, "mine")
                .hide(mContentFragment)
                .hide(mMineFragment)
                .commit();
    }

    private void initEvent() {
        tvMainPage.setOnClickListener(this);
        tvContent.setOnClickListener(this);
        tvMine.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //首页标签点击事件，切换到首页fragment
            case R.id.tv_main_page_clazz_demo_activity:
                showFragment(mMainPageFragment);
                break;
            //内容标签点击事件，切换到内容fragment
            case R.id.tv_content_clazz_demo_activity:
                showFragment(mContentFragment);
                break;
            //我的标签点击事件，切换到我的fragment
            case R.id.tv_mine_clazz_demo_activity:
                showFragment(mMineFragment);
                break;
            default:
                break;
        }
    }

    private void showFragment(Fragment fragment) {
        if (mMainPageFragment == fragment) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .show(mMainPageFragment)
                    .hide(mContentFragment)
                    .hide(mMineFragment)
                    .commit();
        }
        if (mContentFragment == fragment) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .show(mContentFragment)
                    .hide(mMineFragment)
                    .hide(mMainPageFragment)
                    .commit();
        }
        if (mMineFragment == fragment) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .show(mMineFragment)
                    .hide(mContentFragment)
                    .hide(mMainPageFragment)
                    .commit();
        }
    }

    @Override
    public void modifyTitle(String text) {
        tvTitle.setText(text);
    }
}
